package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author Wang + zhang
 * @version 1.0.0
 * @createTime 2020年08月25日
 * @Description: Permission 前端控制器
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 查询所有的权限信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Permission> permissionList = permissionService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS, permissionList);
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @GetMapping("/findPage")
    public Result findByPage(QueryPageBean param){
        log.info("[分页查询]参数:{}",param);
        //RPC调用
        PageResult pageResult = permissionService.findByPage(param);
        log.info("[查询结果]data:{}",pageResult);
        //结果数据返回
        return new Result(true,"获取权限数据成功",pageResult);
    }

    /**
     * 添加
     * @param permission
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        log.info("[权限管理-添加权限]data:{}",permission);
        //RPC调用
        permissionService.add(permission);
        //结果数据返回
        return new Result(true,"添加权限成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Integer id){
        log.info("[权限管理-删除权限]id:{}",id);
        //RPC调用
        permissionService.delete(id);
        //结果数据返回
        return new Result(true,"删除权限成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        //RPC调用
        Permission permission = permissionService.findById(id);
        //结果数据返回
        return new Result(true,"查询权限成功",permission);
    }

    /**
     * 编辑
     * @param permission
     * @return
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Permission permission){
        log.info("[权限管理-编辑]data:{}",permission);
        //RPC调用
        permissionService.edit(permission);
        //结果数据返回
        return new Result(true,"编辑权限成功");
    }
}
